﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "BaseTreeDlg.h"
#include "IDock.h"
#include "IUiManager.h"
#include "DebugMessage.h"
#include "IDockManager.h"

#include "ui_BaseTreeDlg.h"

#include "qevent.h"
#include "GbmpNew.h"
#include "QTreeWidget"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;
using namespace Sample;

#pragma region 界面Qt交互
BaseTreeDlg::BaseTreeDlg(QWidget *parent)
    : QDialog(parent), m_canCheck(true)
{
    m_ui = NEW_AS_OWNER_PTR(Ui::BaseTreeDlg);
    m_ui->setupUi(this);
    m_pQTreeWidget = m_ui->treeWidget;
    QMetaObject::connectSlotsByName(this);
    // 消息处理。
    connect(m_ui->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(OnTreeItemClicked(QTreeWidgetItem*, int)));
}

BaseTreeDlg::~BaseTreeDlg(void){}

void BaseTreeDlg::OnTreeItemClicked(QTreeWidgetItem* pItem, int index)
{
    if (!m_canCheck)
    {
        return;
    }
    bool hasChecked = false;
    bool hasUnChecked = false;
    int topLevelCount = m_ui->treeWidget->topLevelItemCount();
    for (int m = 0; m < topLevelCount; m++)
    {
        QTreeWidgetItem* pTopItem = m_ui->treeWidget->topLevelItem(m);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pTopItem, L"pTopItem为空",L"GDMP",L"2023-12-30");
        for (int n = 0; n < pTopItem->childCount(); n++)
        {
            QTreeWidgetItem* pSubItem = pTopItem->child(n);
            DBG_WARN_AND_RETURN_VOID_UNLESS(pSubItem, L"pSubItem为空",L"GDMP",L"2023-12-30");
            if (pItem == pSubItem)
            {
                WhetherChildItemsAllChecked(pItem, hasChecked, hasUnChecked);

                if (!hasUnChecked)//如果子类全是选中状态，则改为全部未选中
                {
                    pItem->setCheckState(index, Qt::CheckState::Unchecked);
                    for (int i = 0; i < pItem->childCount(); i++)
                    {
                        pItem->child(i)->setCheckState(0, Qt::CheckState::Unchecked);
                    }
                }
                else//否则为全选中
                {
                    pItem->setCheckState(index, Qt::CheckState::Checked);
                    for (int i = 0; i < pItem->childCount(); i++)
                    {
                        pItem->child(i)->setCheckState(0, Qt::CheckState::Checked);
                    }
                }
            }
            else if (pItem->parent() == pSubItem)
            {
                //根据子类的选择，确定header的选择状态
                WhetherChildItemsAllChecked(pSubItem, hasChecked, hasUnChecked);

                if (hasChecked && hasUnChecked)
                {
                    pSubItem->setCheckState(0, Qt::CheckState::PartiallyChecked);
                }
                else if (hasChecked == false)
                {
                    pSubItem->setCheckState(0, Qt::CheckState::Unchecked);
                }
                else if (hasUnChecked == false)
                {
                    pSubItem->setCheckState(0, Qt::CheckState::Checked);
                }
            }
        }
    }
}

bool BaseTreeDlg::WhetherChildItemsAllChecked(QTreeWidgetItem* pItem, bool& hasChecked, bool& hasUnChecked)
{
    hasChecked = false;
    hasUnChecked = false;
    for (int i = 0; i < pItem->childCount(); i++)
    {
        if (pItem->child(i)->checkState(0) == Qt::CheckState::Checked)
        {
            hasChecked = true;
        }
        else if (pItem->child(i)->checkState(0) == Qt::CheckState::Unchecked)
        {
            hasUnChecked = true;
        }
        else
        {
            DBG_WARN(L"错误的选择状态！",L"GDMP",L"2023-12-30");
        }
    }
    return true;
}

void BaseTreeDlg::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Escape: //绕过QDialogESC键默认关闭对话框处理,同时保持消息路由
        event->ignore();
        QWidget::keyPressEvent(event);
        break;

    default:
        QDialog::keyPressEvent(event);
    }
}

IDock * Sample::BaseTreeDlg::GetDock(std::wstring dockId)
{
    IUiManager *pUiManager = IUiManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiManager, L"pUiManager为空",L"GDMP",L"2023-12-30");

    IDockManager * pDockManager = pUiManager->GetDockManager();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDockManager, L"pDockManager为空",L"GDMP",L"2023-12-30");

    IDock* pDock = pDockManager->GetDock(dockId);
    // 这里不做pDock为nullptr的判断
    return pDock;
}

void BaseTreeDlg::SetEnabled(bool bEnable)
{
    setEnabled(bEnable);
}

QSize BaseTreeDlg::sizeHint() const
{
    return QSize(400, 500);
}
#pragma endregion 界面逻辑

